package com.gs.logviewer.gui.frame;

import com.gs.logviewer.core.LogFileParser;
import com.gs.logviewer.gui.comps.LogViewerPanel;
import com.gs.logviewer.model.Log;
import com.gs.utils.text.StringUtil;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author Sabuj
 */
public class ViewLogFileInternalFrame extends LogViewerInternalFrame {

    public ViewLogFileInternalFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
        GridBagConstraints gridBagConstraints;

        mainPanel = new JPanel();
        viewLogToolBar = new JToolBar();
        openLogFileButton = new JButton();
        openLogFileButton.addActionListener(this);
        reloadLogButton = new JButton();
        reloadLogButton.addActionListener(this);
        jSeparator1 = new JToolBar.Separator();
        saveLogButton = new JButton();
        saveLogButton.addActionListener(this);
        saveAllLogButton = new JButton();
        saveAllLogButton.addActionListener(this);
        saveAsLogButton = new JButton();
        saveAsLogButton.addActionListener(this);
        jSeparator2 = new JToolBar.Separator();
        cutLogButton = new JButton();
        cutLogButton.addActionListener(this);
        copyLogButton = new JButton();
        copyLogButton.addActionListener(this);
        pasteLogButton = new JButton();
        pasteLogButton.addActionListener(this);
        selectAllLogButton = new JButton();
        jSeparator5 = new JToolBar.Separator();
        undoButton = new JButton();
        undoButton.addActionListener(this);
        redoButton = new JButton();
        redoButton.addActionListener(this);
        jSeparator3 = new JToolBar.Separator();
        jLabel1 = new JLabel();
        logLevelSlider = new JSlider();
        logLevelSlider.addChangeListener(this);
        currentLogLevelLabel = new JLabel();
        jSeparator4 = new JToolBar.Separator();
        filterLogButton = new JButton();
        filterLogButton.addActionListener(this);
        jPanel1 = new JPanel();
        viewLogTabbedPane = new JTabbedPane();
        viewLogTabbedPane.addChangeListener(this);
        mainPanel.setLayout(new GridBagLayout());

        viewLogToolBar.setFloatable(false);
        viewLogToolBar.setRollover(true);
        viewLogToolBar.setAutoscrolls(true);

        openLogFileButton.setIcon(new ImageIcon(getClass().getResource("/images/folder_open_file.png"))); // NOI18N
        openLogFileButton.setText("Open");
        openLogFileButton.setFocusable(false);
        openLogFileButton.setHorizontalTextPosition(SwingConstants.CENTER);
        openLogFileButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(openLogFileButton);

        reloadLogButton.setIcon(new ImageIcon(getClass().getResource("/images/reload_green.png"))); // NOI18N
        reloadLogButton.setText("Reload");
        reloadLogButton.setFocusable(false);
        reloadLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        reloadLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(reloadLogButton);
        viewLogToolBar.add(jSeparator1);

        saveLogButton.setIcon(new ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveLogButton.setText("Save");
        saveLogButton.setFocusable(false);
        saveLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        saveLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(saveLogButton);

        saveAllLogButton.setIcon(new ImageIcon(getClass().getResource("/images/saveall.gif"))); // NOI18N
        saveAllLogButton.setText("Save All");
        saveAllLogButton.setFocusable(false);
        saveAllLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        saveAllLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(saveAllLogButton);

        saveAsLogButton.setIcon(new ImageIcon(getClass().getResource("/images/saveas_edit.gif"))); // NOI18N
        saveAsLogButton.setText("Save As");
        saveAsLogButton.setFocusable(false);
        saveAsLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        saveAsLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(saveAsLogButton);
        viewLogToolBar.add(jSeparator2);

        cutLogButton.setIcon(new ImageIcon(getClass().getResource("/images/cut.gif"))); // NOI18N
        cutLogButton.setText("Cut");
        cutLogButton.setFocusable(false);
        cutLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        cutLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(cutLogButton);

        copyLogButton.setIcon(new ImageIcon(getClass().getResource("/images/copy.gif"))); // NOI18N
        copyLogButton.setText("Copy");
        copyLogButton.setFocusable(false);
        copyLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        copyLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(copyLogButton);

        pasteLogButton.setIcon(new ImageIcon(getClass().getResource("/images/paste.gif"))); // NOI18N
        pasteLogButton.setText("Paste");
        pasteLogButton.setFocusable(false);
        pasteLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        pasteLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(pasteLogButton);

        selectAllLogButton.setIcon(new ImageIcon(getClass().getResource("/images/selectall.gif"))); // NOI18N
        selectAllLogButton.setText("Select All");
        selectAllLogButton.setFocusable(false);
        selectAllLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        selectAllLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(selectAllLogButton);
        viewLogToolBar.add(jSeparator5);

        undoButton.setText("Undo");
        undoButton.setFocusable(false);
        undoButton.setHorizontalTextPosition(SwingConstants.CENTER);
        undoButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(undoButton);

        redoButton.setText("Redo");
        redoButton.setFocusable(false);
        redoButton.setHorizontalTextPosition(SwingConstants.CENTER);
        redoButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(redoButton);
        viewLogToolBar.add(jSeparator3);

        jLabel1.setText("Log Level: ");
        viewLogToolBar.add(jLabel1);

        logLevelSlider.setForeground(new Color(0, 51, 51));
        logLevelSlider.setMaximum(5);
        logLevelSlider.setPaintLabels(true);
        logLevelSlider.setPaintTicks(true);
        logLevelSlider.setSnapToTicks(true);
        logLevelSlider.setMaximumSize(new Dimension(180, 31));
        logLevelSlider.setMinimumSize(new Dimension(180, 31));
        logLevelSlider.setPreferredSize(new Dimension(180, 31));
        viewLogToolBar.add(logLevelSlider);

        currentLogLevelLabel.setText("DEBUG");
        viewLogToolBar.add(currentLogLevelLabel);
        viewLogToolBar.add(jSeparator4);

        filterLogButton.setIcon(new ImageIcon(getClass().getResource("/images/systemfilterpool.gif"))); // NOI18N
        filterLogButton.setText("Filter");
        filterLogButton.setFocusable(false);
        filterLogButton.setHorizontalTextPosition(SwingConstants.CENTER);
        filterLogButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        viewLogToolBar.add(filterLogButton);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        mainPanel.add(viewLogToolBar, gridBagConstraints);

        jPanel1.setLayout(new BorderLayout());
        viewLogTabbedPane.setMinimumSize(new java.awt.Dimension(480, 400));
        viewLogTabbedPane.setPreferredSize(new java.awt.Dimension(480, 400));
        jPanel1.add(viewLogTabbedPane, BorderLayout.CENTER);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        mainPanel.add(jPanel1, gridBagConstraints);

        getContentPane().add(mainPanel, BorderLayout.CENTER);

        pack();
    }
    private JButton copyLogButton;
    private JLabel currentLogLevelLabel;
    private JButton cutLogButton;
    private JButton filterLogButton;
    private JLabel jLabel1;
    private JPanel jPanel1;
    private JToolBar.Separator jSeparator1;
    private JToolBar.Separator jSeparator2;
    private JToolBar.Separator jSeparator3;
    private JToolBar.Separator jSeparator4;
    private JToolBar.Separator jSeparator5;
    private JSlider logLevelSlider;
    private JPanel mainPanel;
    private JButton openLogFileButton;
    private JButton pasteLogButton;
    private JButton redoButton;
    private JButton reloadLogButton;
    private JButton saveAllLogButton;
    private JButton saveAsLogButton;
    private JButton saveLogButton;
    private JButton selectAllLogButton;
    private JButton undoButton;
    private JTabbedPane viewLogTabbedPane;
    private JToolBar viewLogToolBar;

    public JTabbedPane getViewLogTabbedPane() {
        return viewLogTabbedPane;
    }

    public void setViewLogTabbedPane(JTabbedPane viewLogTabbedPane) {
        this.viewLogTabbedPane = viewLogTabbedPane;
    }

    public JToolBar getViewLogToolBar() {
        return viewLogToolBar;
    }

    public void setViewLogToolBar(JToolBar viewLogToolBar) {
        this.viewLogToolBar = viewLogToolBar;
    }
    private List<File> logFileList = new ArrayList<File>();

    public List<File> getLogFileList() {
        return logFileList;
    }

    public void setLogFileList(List<File> logFileList) {
        this.logFileList = logFileList;
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource().equals(logLevelSlider)) {
            logLevelSliderStateChanged(e);
        } else if (e.getSource().equals(viewLogTabbedPane)) {
            logTabChanged(e);
        }
    }

    private void logLevelSliderStateChanged(ChangeEvent e) {
    }

    private void logTabChanged(ChangeEvent e) {
    }

    public void showLogFile(File logFile, String conversionPattern) {
        if (logFile == null || !StringUtil.hasValidContent(conversionPattern)) {
            return;
        }
        if (!logFile.exists()) {
            return;
        }
        
        try {
			List<Log> logList = LogFileParser.parseLogFile(logFile, conversionPattern);
			getViewLogTabbedPane().addTab(logFile.getName(), new LogViewerPanel(logList));
	        getViewLogTabbedPane().updateUI();
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        
    }
}
